<template>
  <div class="container">
    <!-- 固定导航 -->
    <van-nav-bar fixed left-arrow @click-left="$router.back()" title="小思同学"></van-nav-bar>

    <!-- 聊天主体区域 -->
    <div class="chat-list">
      <div>
          <div class="div" v-for="obj,index in list" :key="index">
               <!-- 左侧是机器人小思 -->
          <div class="chat-item left"  v-if="obj.name === 'xs'" ref="chat-item">
            <van-image fit="cover" round src="https://img.yzcdn.cn/vant/cat.jpeg" />
            <div class="chat-pao">{{obj.msg}}</div>
          </div>

          <!-- 右侧是当前用户 -->
          <div class="chat-item right" ref="chat-item" v-else>
            <div class="chat-pao">{{obj.msg}}</div>
            <van-image fit="cover" round :src="$store.state.user.isObj.photo" />
          </div>
          </div>
       </div>
    </div>

    <!-- 对话区域 -->
    <div class="reply-container van-hairline--top">
      <van-field  placeholder="说点什么..." v-model.trim="content">
        <template #button>
          <span style="font-size:12px;color:#999" @click="sendMsgFn">提交</span>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
// 导入 socket.io-client
import { getToken } from '@/utils/token'
import { io } from 'socket.io-client'
let socket = null
export default {
  name: 'Xiaoai',
  created () {
    // 创建客户端 websocket 的实例
    socket = io('ws://toutiao.itheima.net', {
      query: { token: getToken() },
      transports: ['websocket']
    })
    // 建立连接
    socket.on('connect', () => {
      // 接收小思的回信
      socket.on('message', (data) => {
        // 添加到数组中，name 是 xs
        this.list.push({ ...data, name: 'xs' })
        // 接收的消息也是随之滚到到底部
        this.scrllButon2()
      })
    })
  },
  data () {
    return {
      content: '', // 聊天输入框的值
      // 聊天列表
      list: [
        // 只根据 name 属性，即可判断出这个消息应该渲染到左侧还是右侧
      ]
    }
  },
  methods: {
    // 发送消息
    sendMsgFn () {
      // 判断的内容不能为空
      if (!this.content) return
      // 1. 发送
      socket.emit('message', {
        msg: this.content,
        timestamp: String(Date.now())
      })
      // 2.添加到list中，回显
      this.list.push({
        name: 'me',
        msg: this.content
      })
      // 3. 清空输入框
      this.content = ''
      // 发送消息滚到底部
      this.scrllButon2()
    },
    // 当内容增多的时候随最后一条消息滚动到底部
    scrllButon2 () {
      this.$nextTick(() => {
        const nodes = this.$refs['chat-item']
        // 获取到最后一个对话
        const el = nodes[nodes.length - 1]
        // 原生scrollIntoView()
        el.scrollIntoView({ behavior: 'smooth' })
      })
    }
  },
  // 组件销毁后，把连接断开
  destroyed () {
    socket && socket.close()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: '';
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
